# Copyright (C) Nicola Bizzoca   2006/2007  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006/2007  <ekoes@users.sourceforge.net>
# 
#    This file is part of Absynth-FTP.
#
#    Absynth-FTP is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Absynth-FTP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk, gobject, TreeViewNew

class itemStore:
    
    def __init__(self, item, action):
        self.item, self.action = item, action
        
class radioGroupStore:
    
    def __init__(self, id, item):
        self.root = {id: item}
        self.children = {}
    
    def addChild(self, id, item):
        self.children[id] = item
        
    def getRoot(self):
        return self.root.values()[0]

class genericPopupMenu:
    count = {"menu": 0}
    radios = {}
    
    def __init__(self):
        self.__actions = gtk.ActionGroup("PopupMenuActions-%s" % genericPopupMenu.count["menu"])
        genericPopupMenu.count["menu"] += 1
        self.__accels = gtk.AccelGroup()
        self.items = {}
        self.count["items"] = 0
        self.widget = gtk.Menu()
        
    def __addBaseEntry(self, value, desc, stock, accel, key):
        label = key and value.replace(key, "_%s" % key) or value
        action = gtk.Action(value, label, desc, stock)
        self.__actions.add_action_with_accel(action, accel)
        action.set_accel_group(self.__accels)
        return action
        
    def addSeparatorEntry(self):
        item = gtk.SeparatorMenuItem()
        item.show()
        self.__appendNewEntry(item, None, None)
        
    def addSimpleEntry(self, value, desc, stock, accel, key, callback = None):
        action = self.__addBaseEntry(value, desc, stock, accel, key)
        item = action.create_menu_item()
        self.__appendNewEntry(item, action, callback)
        
    def addCheckEntry(self, value, desc, accel, key, callback = None, checked = False):
        action = self.__addBaseEntry(value, desc, None, accel, key)
        action.set_menu_item_type(gtk.CheckMenuItem)
        item = action.create_menu_item()
        item.set_active(checked)
        self.__appendNewEntry(item, action, callback)
        
    def addRadioEntry(self, value, desc, accel, key, group, callback = None, checked = False):
        action = self.__addBaseEntry(value, desc, None, accel, key)
        action.set_menu_item_type(gtk.RadioMenuItem)
        item = action.create_menu_item()
        itemstore = itemStore(item, action)
        if group in genericPopupMenu.radios.keys():
            item.set_group(genericPopupMenu.radios[group].getRoot().item)
            genericPopupMenu.radios[group].addChild(self.count["items"], itemstore)
        else:
            genericPopupMenu.radios[group] = radioGroupStore(self.count["items"], itemstore)
        item.set_active(checked)
        self.__appendNewEntry(item, action, itemstore)
        
    def __appendNewEntry(self, item, action, callback, itemstore = None):
        item.connect("activate", self.__onEntryClick, callback)
        self.items[self.count["items"]] = True and itemstore or itemStore(item, action)
        self.count["items"] += 1
        self.widget.append(item)
        
    def linkTo(self, widget, handler):
        if handler == None:
            handler == self.__eventHandler()
        widget.connect_object("event", handler, self.widget)
        
    def __eventHandler(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
            self.widget.popup(None, None, None, event.button, event.time)
        
    def __onEntryClick(self, widget, callback):
        if callable(callback): callback()
        

class genericMenuBar:
    
    
    def __init__(self):
        self.__actions = gtk.ActionGroup("MenuBarActions")
        #self.__accels = gtk.AccelGroup()
        self.items = {}
        self.count = {"items": 0}
        self.__store = {}
        self.widget = gtk.MenuBar()
        
    def addMenuEntry(self, value, key, submenu):
        label = key and value.replace(key, "_%s" % key) or value
        action = gtk.Action(key, label, None, None)
        self.__actions.add_action(action)
        item = action.create_menu_item()
        item.set_submenu(submenu.widget)
        self.__appendNewEntry(item, action, value)
    
    def __appendNewEntry(self, item, action, value):
        self.items[self.count["items"]] = itemStore(item, action)
        self.__store[value] = self.items[self.count["items"]]
        self.count["items"] += 1
        self.widget.append(item)
        
    def getByName(self, value):
        return self.__store[value]
        
class testSuite:
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.connectEvents(self.window, {
             "delete_event" : self.delete,
             "destroy": self.destroy,
        })
        
        self.tree = TreeViewNew.genericTreeView({
            "headers-visible"  : True,
            "headers-clickable": True,
            "rows-search"      : False,
            "rows-same-height" : False,
            "rows-alternate"   : True,
            "rows-reorder"     : False,
        })
        
        self.tree.addImageColumn("Filename", True, True)
        self.tree.addTextColumn("-", str, False, False)
        self.tree.addTextColumn("Size", str, True, False, 50)
        self.tree.addTextColumn("Last Edit", str, True, True)
        self.tree.mergeColumns(0, 1, None, None)
                
        self.tree.addRow(["stock_folder", "usr", "4096", "aee"])
        self.tree.addRow(["stock_folder", "bin", "4096", "?"], parent = 0)
        self.tree.addRow(["stock_folder", "etc", "4096", "?"], parent = 0)
        self.tree.addRow(["stock_folder", "var", "4096", "?"], parent = 0)
        self.tree.addRow(["stock_folder", "local", "4096", "?"], parent = 0, position = 1)
        self.tree.addRow(["stock_folder", "bin", "4097", "beee"])
        
        self.tree.columns[0].rend.set_property('xalign', 1.0)
        self.tree.columns[0].col.set_sort_column_id(1)
        
        self.menu = [genericPopupMenu(), genericPopupMenu()]
        
        self.menu[0].addSimpleEntry("Nuovo", "Crea nuovo file", gtk.STOCK_NEW, "<control>N", "N")
        self.menu[0].addSimpleEntry("Apri", "Apri file esistente", gtk.STOCK_OPEN, "<control>O", "O")
        self.menu[0].addSimpleEntry("Salva", "Salva file", gtk.STOCK_SAVE, "<control>S", "S", prova)
        self.menu[0].addSeparatorEntry()
        self.menu[0].addSimpleEntry("Esci", "Rimuovi file", gtk.STOCK_QUIT, "<control>Q", "Q")


        self.menu[1].addSimpleEntry("Homepage", "Apri da homepage", gtk.STOCK_HOME, "<control>H", "H")
        self.menu[1].addSimpleEntry("Network", "Apri da network", gtk.STOCK_NETWORK, "<control>R", "r")
        self.menu[1].addSimpleEntry("Preferenze", "Apri da preferenze", gtk.STOCK_PREFERENCES, "<control>P", "P")
        self.menu[1].addSimpleEntry("Altro", "", None, None, None)
        self.menu[1].addSeparatorEntry()
        self.menu[1].addCheckEntry("Mostra files nascosti", "Mostra i files nascosti", "<Alt>f", "f", prova, True)
        self.menu[1].addSeparatorEntry()
        self.menu[1].addRadioEntry("Visualizza per nome", "Visualizza i file per nome", "<Alt>m", "m", "radio", None, True)
        self.menu[1].addRadioEntry("Visualizza per ultima modifica", "Visualizza i file per ultima modifica", "<Alt>u", "u", "radio")
        self.menu[1].addRadioEntry("Visualizza per dimensioni", "Visualizza i file per dimensioni", "<Alt>d", "d", "radio", prova)
        
        self.menu[0].items[1].item.set_submenu(self.menu[1].widget)
        self.menu[0].linkTo(self.tree.widget)
        
        self.window.add(self.tree.widget)
        self.window.show_all()
    
    def connectEvents(self, widget, events):
        for type,callback in events.items():
            widget.connect(type, callback)

    def delete(self, widget, event, data=None):
        #print "delete event occurred"
        return False

    def destroy(self, widget, data=None):
        #print "destroy signal occurred"
        gtk.main_quit()
        
    def runTest(self):
        gtk.main()
        
def prova():
    print "ciao"
        
if __name__ == "__main__": 
    GTK = testSuite()
    GTK.runTest()